package com.test.demolabel.Activity;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.zxing.client.android.ZXingUtil;
import com.test.demolabel.Manaer.PrintfManager;
import com.test.demolabel.Modle.Mode;
import com.test.demolabel.R;

import java.util.List;

public class MainActivity extends Activity {

    private TextView tv_main_bluetooth,btn_create;
    private Button btn_main_test_p26,btn_main_test_p16;
    private ImageView imageView;
    private List<Mode> listData;
    private final int ImageWidth = 380;
    private final int QRCodeWidth = 280;
    private PrintfManager printfManager;

    private Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        context = this;
        initView();
        initData();
        setLister();
    }

    private Bitmap decodeResource(Resources resources, int id) {
        TypedValue value = new TypedValue();
        resources.openRawResource(id, value);
        BitmapFactory.Options opts = new BitmapFactory.Options();
        opts.inTargetDensity = value.density;
        return BitmapFactory.decodeResource(resources, id, opts);
    }

    private void setLister() {

        btn_main_test_p26.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Bitmap bitmap = decodeResource(getResources(), R.mipmap.p_one_six);
                printfManager.printf(48, 50, bitmap, MainActivity.this);
            }
        });

        //识别不了，号，可以输入英（.!?）中的（！，？。、）
        btn_main_test_p16.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                Bitmap bitmap = decodeResource(getResources(),R.mipmap.p_one_six);
//                printfManager.printf(48,50,bitmap,MainActivity.this);
//                printfManager.TestPrintfText(48,50,"\"买?砖宝123adb\"",MainActivity.this);
                printfManager.TestPrintfQRCode(48, 50, "\"123adb\"", MainActivity.this);
            }
        });

        printfManager.addBluetoothChangLister(new PrintfManager.BluetoothChangLister() {
            @Override
            public void chang(String name, String address) {
                tv_main_bluetooth.setText(name);
            }
        });

        tv_main_bluetooth.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PrintfBlueListActivity.startActivity(MainActivity.this);
            }
        });


        btn_create.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Bitmap bitmap = createQRCODE("http://www.hao123.com", "买砖宝买砖宝买砖宝");
                imageView.setImageBitmap(bitmap);
                printfManager.printf(48,50,bitmap,MainActivity.this);
            }
        });

    }


    private Bitmap createQRCODE(String url, String content) {
        Bitmap bitmap = Bitmap.createBitmap(ImageWidth, ImageWidth, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        canvas.drawColor(Color.WHITE);

        Bitmap bitmap1 = ZXingUtil.encodeQRCode(url, QRCodeWidth, QRCodeWidth);
        canvas.drawBitmap(bitmap1, (ImageWidth-QRCodeWidth)/2, (ImageWidth-QRCodeWidth)/5, new Paint());

        Paint paint = new Paint();
        paint.setColor(Color.BLACK);
        paint.setTextSize(dip2px(MainActivity.this, 16));
        paint.setAntiAlias(true);
        float fontWidth = paint.measureText(content);
        canvas.drawText(content, (ImageWidth - fontWidth) / 2, QRCodeWidth+((ImageWidth-QRCodeWidth)*2/5), paint);
        return bitmap;
    }


    private void initData() {
        printfManager = PrintfManager.getInstance(context);
        printfManager.defaultConnection();
    }

    private void initView() {
        btn_main_test_p26 = (Button) findViewById(R.id.btn_main_test_p26);
        btn_main_test_p16 = (Button) findViewById(R.id.btn_main_test_p16);
        tv_main_bluetooth = (TextView) findViewById(R.id.tv_main_bluetooth);
        btn_create = (Button) findViewById(R.id.btn_create);
        imageView = (ImageView) findViewById(R.id.iv_image);
    }

    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }
}
